package cn.edu.jmu.oelp.controller;

import cn.edu.jmu.oelp.pojo.EntityRelation;
import cn.edu.jmu.oelp.pojo.ReqRelation;
import cn.edu.jmu.oelp.pojo.RespCode;
import cn.edu.jmu.oelp.pojo.RespEntity;
import cn.edu.jmu.oelp.service.EntityNeo4jService;
import cn.edu.jmu.oelp.service.EntityRelationNeo4jService;
import cn.edu.jmu.oelp.service.EntityRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Transactional(rollbackFor=Exception.class)
public class EntityRelationController {
    @Autowired
    private EntityNeo4jService entityNeo4jService;
    @Autowired
    private EntityRelationService entityRelationService;
    @Autowired
    private EntityRelationNeo4jService entityRelationNeo4jService;

    @RequestMapping("/addRelation")
    public RespEntity addRelation(@RequestBody ReqRelation reqRelation){
        try {
            if(reqRelation==null){
                return new RespEntity(RespCode.NULLDATA);
            }
            //根据标注内容获取id
            long startId = entityNeo4jService.findByArticleIdAndParagraphsIdAndContentAndUserId(reqRelation.getArticleId(),reqRelation.getParagraphsId(),reqRelation.getStartContent(), reqRelation.getUserId()).getEntityId();
            long endId = entityNeo4jService.findByArticleIdAndParagraphsIdAndContentAndUserId(reqRelation.getArticleId(),reqRelation.getParagraphsId(),reqRelation.getEndContent(), reqRelation.getUserId()).getEntityId();
            reqRelation.setStartId(startId);
            reqRelation.setEndId(endId);
            //根据id实例化关系
            EntityRelation entityRelation = new EntityRelation(reqRelation.getStartId(),reqRelation.getEndId(),reqRelation.getType());
            if(entityRelation.getEndId()==null||entityRelation.getStartId()==null||entityRelation.getType()==null)
            {
                return new RespEntity(RespCode.LACKDATA);
            }
            else if(entityRelationService.addRelation(entityRelation)>0)
            {
                if(entityRelationNeo4jService.addRelation(entityRelation) != null)
                {
                    System.out.println("add relation to neo4j");
                    return new RespEntity(RespCode.SUCCESS,entityRelation);
                }
                System.out.println("123");
                return new RespEntity(RespCode.ADDERROR);
            }
        }
        catch (Exception e)
        {
            System.out.println(e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        System.out.println("456");
        System.out.println(reqRelation);
        return new RespEntity(RespCode.ADDERROR);
    }
    @RequestMapping("/delRelation")
    public RespEntity delRelation(int id){
        if(entityRelationService.delRelation(id)>0)
        {
            entityRelationNeo4jService.delRelation((long) id);
            return new RespEntity(RespCode.SUCCESS);
        }
        return new RespEntity(RespCode.DELERROR);
    }
    @RequestMapping("/updateRelation")
    public RespEntity updateRelation(@RequestBody EntityRelation entityRelation){
        if(entityRelation==null){
            return new RespEntity(RespCode.NULLDATA);
        }
        if(entityRelation.getEndId()==null||entityRelation.getStartId()==null||entityRelation.getType()==null)
            return new RespEntity(RespCode.LACKDATA);
        else if(entityRelationService.updateRelation(entityRelation)>0){
            entityRelationNeo4jService.updateRelation(entityRelation);
            return new RespEntity(RespCode.SUCCESS,entityRelation);
        }
        return new RespEntity(RespCode.UPDATEERROR);
    }
    @RequestMapping("/findAllRelation")
    public RespEntity findAll(){
        if(entityRelationService.findAll()==null)
            return new RespEntity(RespCode.NULLDATA);
        return new RespEntity(RespCode.SUCCESS,entityRelationService.findAll());
    }
    @RequestMapping("/findRelationById")
    public RespEntity findById(int id){
        if(entityRelationService.findById(id)==null)
            return new RespEntity(RespCode.NULLDATA);
        return new RespEntity(RespCode.SUCCESS,entityRelationService.findById(id));
    }
    @RequestMapping("/findRelationByStartEntityId")
    public RespEntity findByStartEntityId(long id){
        if(entityRelationService.findByStartEntityId(id)==null)
            return new RespEntity(RespCode.NULLDATA);
        return new RespEntity(RespCode.SUCCESS,entityRelationService.findByStartEntityId(id));
    }
    @RequestMapping("/findRelationByEndEntityId")
    public RespEntity findByEndEntityId(long id){
        if(entityRelationService.findByEndEntityId(id)==null)
            return new RespEntity(RespCode.NULLDATA);
        return new RespEntity(RespCode.SUCCESS,entityRelationService.findByEndEntityId(id));
    }
    @RequestMapping("/findRelationByType")
    public RespEntity findByType(String type){
        if(entityRelationService.findByType(type)==null)
            return new RespEntity(RespCode.NULLDATA);
        return new RespEntity(RespCode.SUCCESS,entityRelationService.findByType(type));
    }

    @RequestMapping("/findRelationByStartEntityIdAndEndEntityId")
    public RespEntity findByStartEntityIdAndEndEntityId(long startEntityId, long endEntityId){
        if(entityRelationService.findByStartEntityIdAndEndEntityId(startEntityId,endEntityId)==null)
            return new RespEntity(RespCode.NULLDATA);
        return new RespEntity(RespCode.SUCCESS,entityRelationService.findByStartEntityIdAndEndEntityId(startEntityId,endEntityId));
    }
    @RequestMapping("/findRelationByStartEntityIdAndEndEntityIdAndType")
    public RespEntity findByStartEntityIdAndEndEntityIdAndType(long startEntityId, long endEntityId, String type){
       if(entityRelationService.findByStartEntityIdAndEndEntityIdAndType(startEntityId,endEntityId,type)==null)
            return new RespEntity(RespCode.NULLDATA);
        return new RespEntity(RespCode.SUCCESS,entityRelationService.findByStartEntityIdAndEndEntityIdAndType(startEntityId,endEntityId,type));
    }
//    @RequestMapping("/submitRelation")
//    public RespEntity submitRelation(@RequestBody ReqRelation reqRelation){
//        try {
//            if(reqRelation==null){
//                return new RespEntity(RespCode.NULLDATA);
//            }
//
//            EntityRelation entityRelation = new EntityRelation(reqRelation.getStartId(),reqRelation.getEndId(),reqRelation.getType());
//            if(entityRelation.getEndId()==null||entityRelation.getStartId()==null||entityRelation.getType()==null)
//            {
//                return new RespEntity(RespCode.LACKDATA);
//            }
//            else if(entityRelationService.addRelation(entityRelation)>0)
//            {
//                if(entityRelationNeo4jService.addRelation(entityRelation) != null)
//                {
//                    System.out.println("add relation to neo4j");
//                    return new RespEntity(RespCode.SUCCESS,entityRelation);
//                }
//                return new RespEntity(RespCode.ADDERROR);
//            }
//        }
//        catch (Exception e)
//        {
//            System.out.println(e);
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }
//        return new RespEntity(RespCode.ADDERROR);
//    }
}
